<?
session_start();
require_once("./config.php");
require_once("./mysql.php");
$table="stats";
$opt=$_GET["opt"];
function fakeonline()
{
	global $timeLoadOnline,$table,$opt;
	$h=intval(date("H"));
	$m=intval(date("i"));
	$dataH=array(
		0 => 1000,
		1 => 500,
		2 => 20,
		3 => 10,
		4 => 30,
		5 => 50,
		6 => 200,
		7 => 500,
		8 => 1500,
		9 => 4000,
		10 => 5800,
		11 => 6000,
		12 => 6200,
		13 => 5500,
		14 => 6300,
		15 => 7000,
		16 => 5500,
		17 => 4000,
		18 => 4500,
		19 => 6500,
		20 => 8700,
		21 => 6000,
		22 => 3500,
		23 => 2000,
	);
	$ret=$dataH[$h];
	$value=$dataH[$h+1]-$dataH[$h];
	$value= ceil($value/60);
	$ret+=$m*$value;
	
	if(time()-$_SESSION["lastload"]>60){
		$_SESSION["rand"]= rand(-20,20);
	}
	
	$ret+=$_SESSION["rand"];
	$_SESSION["lastload"]=time();
	if($ret <1) $ret =1;
	return $ret;
}
function showstats(){
	global $table;
	$q=query("select * from other where code='stats'");
	$r=fetch($q);
	$data["total_session"]=number_format($r["content1"]);
	$data["total_reload"]=number_format($r["content2"]);
	$q=query("select ipaddr from $table");
	$data["online"]=numrows($q);
	$data["online1"]=$data["online"];
	//fake số lượng người online
	$data["online"]+=fakeonline();
	$data["online"]=number_format($data["online"]);
	return $data;
}
function updatedata()
{
	global $timeLoadOnline,$table,$opt;
	$time=time()-$timeLoadOnline*60;
	$ss=session_id();
	$q=query("select * from $table where ipaddr='$ss'");
	if(numrows($q)==0)
	{
		$data=array("ipaddr" => "$ss", "time" => time());
		insert($data,$table);
		query("update other set content1=content1+1,content2=content2+1 where code='stats'");
	}
	else
	{
		update(array("time"=> time()),$table,"ipaddr='$ss'");
	}
	query("delete from $table where time< '$time' ");
}
updatedata();
$data=showstats();
echo json_encode($data);
?>